package com.lune.job.controller;


import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.framework.role.RequireRoles;
import com.lune.job.framework.role.Role;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.Resume;
import com.lune.job.service.IResumeService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/resume")
@Api(tags = "简历管理")
public class ResumeController {


    @Autowired
    private IResumeService resumeService;

    @Autowired
    private RedisUtil redisUtil;

    @RequireRoles(type = Role.STUDENT)
    @ApiOperation(value = "添加简历")
    @PostMapping("/")
    public RespBean create(@RequestBody Resume resume){
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        resume.setStudent_id(userData.getId());
        boolean flag = resumeService.save(resume);
        if (flag){
            return RespBean.success("添加简历成功");
        }else {
            return RespBean.error("添加简历失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除简历")
    public RespBean delete(@PathVariable Integer id) {
        if (resumeService.removeById(id)) {
            return RespBean.success("删除简历成功");
        } else {
            return RespBean.error("删除简历失败");
        }
    }

    @DeleteMapping("/")
    @ApiOperation(value = "批量删除简历")
    public RespBean deleteByIds(Integer[] ids) {
        if (resumeService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除简历成功");
        } else {
            return RespBean.error("批量删除简历失败");
        }
    }

    @PutMapping("/")
    @ApiOperation(value = "修改简历")
    public RespBean update(@RequestBody Resume resume) {
        if (resumeService.updateById(resume)) {
            return RespBean.success("修改简历成功");
        } else {
            return RespBean.error("修改简历失败");
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询简历信息")
    public RespBean detail(Integer id) {
        Resume byId = resumeService.getById(id);
        if (byId != null) {
            return RespBean.success("查询成功", byId);
        } else {
            return RespBean.error("查询失败");
        }
    }

    @GetMapping("/queryPage")
    @ApiOperation(value = "分页查询简历信息")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 Resume resume) {
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        resume.setStudent_id(userData.getId());
        return resumeService.queryPage(currentPage, size, resume);
    }

    @PostMapping("/queryByCon")
    @ApiOperation(value = "条件查询简历信息")
    public List<Resume> queryByCon(@RequestBody  Resume resume) {
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        resume.setStudent_id(userData.getId());
        return resumeService.queryByCon(resume);
    }


}
